// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[]  array;

    public HashBucket() {
        // write code here
        this.array = new Node[DEFAULT_SIZE];
    }

    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 10;//默认桶的大小

    public int put(int key, int value) {
        // write code here
        int index=key%array.length;
        //找是否有相同的key如果有就不插入了
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                return -1;
            }
            cur=cur.next;
        }
        //头插法插数
        Node node=new Node(key,value);
        node.next=array[index];
        array[index]=node;
        size++;
        if(loadFactor()>DEFAULT_SIZE){
            resize();
        }
        return 0;
    }


    private void resize() {
        // write code here
        Node[]tmp=new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while(cur!=null){
                Node curNext=cur.next;
                int newIndex=cur.key% tmp.length;
                cur.next=array[newIndex];
                array[newIndex]=cur;
                cur=curNext;
            }
        }
        array=tmp;

    }


    private double loadFactor() {
        return size * 1.0 / array.length;
    }

    public int get(int key) {
         // write code here
        int index=key%array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                return cur.value;
            }
            cur=cur.next;
        }
        return -1;
    }

    public static void main(String[] args) {
        HashBucket hashBucket=new HashBucket();
        hashBucket.put(0,1);
        hashBucket.put(1,1);
        hashBucket.put(2,2);
        hashBucket.put(3,3);
        hashBucket.put(11,1);
        hashBucket.put(12,2);
        hashBucket.put(13,3);
        hashBucket.put(111,1);
        hashBucket.put(1111,1);
        System.out.println("");



    }
}